<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门诊卡id" prop="outpatientid">
        <el-input
          v-model="queryParams.outpatientid"
          placeholder="请输入门诊卡id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="receiverecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="接诊记录表id" align="center" prop="opdoctorreceiverecordid" />
      <el-table-column label="门诊卡id" align="center" prop="outpatientid" />
      <el-table-column label="医生ID" align="center" prop="doctorid" />
        <el-table-column label="患者姓名" align="center" prop="user_name" />
        <el-table-column label="身份证号" align="center" prop="code" />
      <el-table-column label="1门诊2住院" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_reception_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="接诊状态" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_username" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="押金" align="center" prop="deposit" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div v-if="scope.row.type === 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >提交押金</el-button>
          </div>
          <div v-if="scope.row.type === 2">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleAdd(scope.row)"
          >分配</el-button>
           </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改住院患者信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门诊卡id" prop="outpatientid">
          <el-input disabled v-model="form.outpatientid" />
        </el-form-item>
        <el-form-item label="医生ID" prop="doctorid">
          <el-input v-model="form.doctorid" disabled />
        </el-form-item>
        <el-form-item label="身份证号" prop="doctorid">
          <el-input v-model="form.code" disabled />
        </el-form-item>
        <el-form-item label="押金" prop="doctorid">
          <el-input :value="form.type == 2?form.deposit:cash"  disabled />
        </el-form-item>
        <div v-if="form.type == 2">
          <el-form-item label="病区">
            <el-select v-model="form.bcname" @change="selectQuery(form.bcname)"  placeholder="请选择">
              <el-option v-for="dict in Query"
              :key="dict.id"
              :label="dict.name"
              :value="dict.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="病房">
            <el-select v-model="form.bfname"  @change="selectQueryAll(form.bfname)"  placeholder="请选择">
              <el-option v-for="dict in list"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="床位">
            <el-select v-model="form.bedid"  placeholder="请选择">
              <el-option v-for="dict in ms"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" />
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listReceiverecord, getReceiverecord, delReceiverecord, addReceiverecord, updateReceiverecord} from "@/api/be/receiverecord";
import { listArea} from '@/api/be/area.js';
import {query} from "@/api/be/sickroom";
import { queryAll } from "@/api/be/bed";
import {addSplitbed} from "@/api/be/splitbed";
export default {
  name: "Receiverecord",
  dicts: ['sys_reception_status',"sys_username"],
  data() {
    return {
      areaname:"",
      //押金
      cash:100,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 住院患者信息表格数据
      receiverecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outpatientid: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      Query:{},
      list:{},
      ms:{}
    };
  },
  created() {
    this.getList();
    this.Querybc()
  },
  methods: {
    /** 查询住院患者信息列表 */
    getList() {
      this.loading = true;
      listReceiverecord(this.queryParams).then(response => {

        this.receiverecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        opdoctorreceiverecordid: null,
        outpatientid: null,
        doctorid: null,
        status: 0,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /* 病区改变事件 */
    selectQuery(obj){
      this.areaname = obj;
      query(this.areaname).then(response=>{
        if(response.rows == ""){
          this.form.bfname = ""
          this.form.bedId = ""
        }
          this.list = response.rows;
      })
    },
    /*  床位改变事件 */
    selectQueryAll(obj){
      queryAll(obj).then(response=>{
        if(response.rows == ""){
          this.form.cwid = ""
        }
          this.ms = response.rows
      })
    },
    /* 病区查询 */
    Querybc(){
      listArea(this.queryParams).then(response=>{
           this.Query = response.rows;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.opdoctorreceiverecordid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      const opdoctorreceiverecordid = row.opdoctorreceiverecordid || this.ids
      getReceiverecord(opdoctorreceiverecordid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "分配病房";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const opdoctorreceiverecordid = row.opdoctorreceiverecordid || this.ids
      getReceiverecord(opdoctorreceiverecordid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "提交押金";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.type == 1) {
            this.form.deposit = parseFloat(this.form.deposit)+parseFloat(this.cash);
            updateReceiverecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form)
            addSplitbed(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              console.log(response)
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
